package com.oygo.hunter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.oygo.hunter.bean.KeepWrapperBean;
import com.oygo.hunter.decode.DecodeWrapper;
import com.register.DJobService;
import com.vi.daemon.DaemonNative;
import com.oygo.hunter.keep.IKeepLive;
import com.oygo.hunter.keep.ProcessHolder;
import com.oygo.hunter.receiver.ScreenReceiver;
import com.oygo.hunter.utils.AccountUtils;
import com.oygo.hunter.utils.OSUtils;
import com.oygo.hunter.utils.ProcessUtils;
import com.oygo.hunter.utils.ServiceUtils;

import java.io.File;

public class KeepLive {

    public KeepWrapperBean keepWrapperBean;
    public Context mContext;
    //    public IPowerManager pm;
    public IKeepLive iKeep;


    public class Keep_Runable1 implements Runnable {
        public class Runnable1Start implements Runnable {

            @Override
            public void run() {
                Log.d("cql", "start service");
                ServiceUtils.startImpl();
            }
        }

        @Override
        public void run() {
            int v0 = 0;
            Process.setThreadPriority(-2);
            try {
                while (true) {
                    if ((KeepLive.this.mContext.getPackageManager().getApplicationInfo(KeepLive.this.mContext.getPackageName(), 0x80).flags & 0x200000) == 0) {
                        if (v0 >= 3) {
                            break;
                        } else {
                            new Thread(new Runnable1Start()).start();
                        }
                    }
                    v0++;
                }
            } catch (Throwable unused_ex) {
            }
        }
    }

    private void kp_synchronized() {
        if (this.keep_extends()) {
            new Thread(new Keep_Runable1()).start();
        }
    }


    private KeepLive(Context context) {

    }

    public static final class KeepLiveInner {
        @SuppressLint({"StaticFieldLeak"})
        public static KeepLive instance;

        static {
            KeepLiveInner.instance = new KeepLive(null);
        }
    }


    public static KeepLive getInstance() {
        return KeepLiveInner.instance;
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void start(Context arg3, KeepWrapperBean arg4, IKeepLive arg5) {
        this.mContext = arg3;
        this.keepWrapperBean = arg4;
        this.iKeep = arg5;
        ProcessUtils.init(arg3);
        ProcessHolder.init(arg3);
        if (ProcessUtils.p_package) {
            this.deleteCache();
        }
        Log.d("cql", "start");
        if ((ProcessUtils.p_package) || (ProcessUtils.p_float)) {
            this.registReceiver();
            this.kp_synchronized();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    DaemonNative.forkChild(ProcessHolder.holder_float(), ProcessHolder.holder_volatile(KeepLive.this.mContext), ProcessHolder.holder_private(KeepLive.this.mContext), ProcessHolder.holder_package(KeepLive.this.mContext), ProcessHolder.holder_throws(KeepLive.this.mContext));
                }
            }).start();
        }

        ProcessHolder.holder_extends(arg3);
        //这里有个Power的处理
//        if(((ProcessUtils.p_package) || (ProcessUtils.p_volatile)) && (this.keepWrapperBean.getP1())) {
//            if(ProcessUtils.p_package) {
//                final.throws().float(this.pm);
//            }
//            else {
//                final.float(1000);
//            }
//
//            PowerWrapper.volatile();
//        }

//        DJobService.d_float(arg3);
        if (ProcessUtils.p_package) {
            new Handler(Looper.getMainLooper()).postDelayed(() -> new Thread(new AccountRunnable()).start(), 3000L);
        }
    }


    public class AccountRunnable implements Runnable {

        @Override
        public void run() {
            AccountUtils.acc_package(KeepLive.this.mContext);
        }
    }


    private void registReceiver() {
        IntentFilter v0 = new IntentFilter();
        v0.addAction(DecodeWrapper.package_decode1("VDPrTmd2E4IhM/9WWYa3s7DzNgrIn3d5swwNBFFY2Do="));
        v0.addAction(DecodeWrapper.package_decode1("VDPrTmd2E4IhM/9WWYa3s6fM0NWwEEWTeGUb0jfOf9cdGvzzd7oJwGTrFQRC0aP0"));
        v0.addAction(DecodeWrapper.package_decode1("VDPrTmd2E4IhM/9WWYa3sxhtNaeUGQOohYJMxWj9KHFoqQUNydcR+eWqeGyCqWgV"));
        v0.addAction(DecodeWrapper.package_decode1("VDPrTmd2E4IhM/9WWYa3s4zrBOyuZvq+7V30l3TO9MdiHG8c0qpENDLtTbVj6pt3"));
        v0.addAction(DecodeWrapper.package_decode1("VDPrTmd2E4IhM/9WWYa3sz9Mlo6EUZhl+gPWY5o9qDXeLZuWjP/V5n/aeYvKG3EV"));
        this.mContext.registerReceiver(new ScreenReceiver(), v0);
    }

    private boolean keep_extends() {
        return OSUtils.os_implements() && Build.VERSION.SDK_INT < 29;
    }


    private void deleteCache() {
        for (Object v1 : ProcessHolder.holder_float(mContext)) {
            String v1_1 = (String) v1;
            if (v1_1 == null) {
                continue;
            }

            new File(v1_1).delete();
        }
    }


    public Context getContext() {
        return this.mContext;
    }

    public KeepWrapperBean getWrapperBean() {
        return this.keepWrapperBean;
    }

    public IKeepLive getIkeep() {
        return this.iKeep;
    }
}
